#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/11/26 0:41
# @Author  : chenyj
# @File    : DataFramePart.py
# @Software: IntelliJ IDEA

import pandas as pd


def create_df():
    """
    Creat a DataFrame
    """
    df = pd.DataFrame({'Name': ["chenyj", "liuzp", "chenjy"], 'Age': [32, 31, 3]})
    return df


def filter_age_df(df, condition : int):
    """
    Filter the dataframe to only contain entries with a certain age
    :param df: The dataframe to be filtered
    :param condition: The age condition to filter by
    :return: The filtered dataframe
    """
    df = df[df["Age"] > condition]
    print(df)
    return df


def group_df(df):
    """
    Group the dataframe by name and calculate the mean age
    :param df: The dataframe to be grouped
    :return: The grouped dataframe
    """
    df = df.groupby("Name")["Age"].mean()
    print(df)
    return df


def check_for_missing_data_and_fill(df):
    """
    Check for missing data in the dataframe
    :param df:  to be checked
    :return: True if there are missing data, False otherwise
    """
    missing_values = df.isnull().values.any()
    df["Age"].fillna(0, inplace=True)
    return df


def funApplyToColumn(df):
    """
    Apply a function to a column in the dataframe
    :param df:  to be modified
    :return: The modified dataframe
    """
    df["Age"] = df["Age"].apply(lambda x: x + 1)
    return df


def concat_df(df1, df2):
    """
    Concatenate two dataframes
    :param df1:  to be concatenated
    :param df2:  to be concatenated
    :return: The concatenated dataframe
    """
    df1 = pd.DataFrame({'A': ["A0", "A1"], 'B': ["B0", "B1"]})
    df2 = pd.DataFrame({'A': ["A2", "A3"], 'B': ["B2", "B3"]})
    print(df1)
    print(df2)
    df = pd.concat([df1, df2], ignore_index=True)
    print(df)
    return df


def merge_df():
    """
    Merge two dataframes
    :param df1:  to be merged
    :param df2:  to be merged
    :return: The merged dataframe
    """
    left = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],"value": [1, 2, 3, 4]})
    right = pd.DataFrame({'key': ['K2', 'K3', 'K4', 'K5'], "value": [5, 6, 7, 8]})
    df = pd.merge(left, right, on='key',how='inner')
    print(df)
    return df


def pivot_table_creat(df):
    """
    Create a pivot table
    :param df:  to be pivoted
    :return: The pivoted dataframe
    """
    pivot_table = df.pivot_table(index='Name', columns='Age', values='Age')
    print(pivot_table)
    return pivot_table


def convert_to_datetime(df):
    """
    Convert a dataframe to a CSV file
    :param df:  to be converted
    :return: None
    """
    df["Date"] = df.to_datetime(df["Date"])


def melt_df(df):
    """
    Melt a dataframe
    :param df:  to be melted
    :return: The melted dataframe
    """
    df = pd.melt(df, id_vars=["Name"], value_vars=["Age"])
    print(df)
    return df


def astype_categoty(df):
    df["Name"] = df["Name"].astype("category")
    df["Name"] = df["Name"].cat.codes
    print(df)
    return df


def sample_rows(df):
    """
    Sample rows from a dataframe
    :param df:  to be sampled
    :return: The sampled dataframe
    """
    df = df.sample(n=2)
    print(df)
    return df


def cumulative_sum(df):
    """
    Calculate the cumulative sum of a dataframe
    :param df:  to be summed
    :return: The cumulative sum dataframe
    """
    df["Cumulative"] = df["Values"].cumsum()
    print(df)
    return df


def remove_duplicates(df):
    """
    Remove duplicates from a dataframe
    :param df:  to be deduplicated
    :return: The deduplicated dataframe
    """
    df = df.drop_duplicates(subset=["Col1", "Col2"], keep="first", inplace=True)
    print(df)
    return df


def dummy_variables(df):
    """
    Create dummy variables from a dataframe
    :param df:  to be converted
    :return: The converted dataframe
    """
    df = pd.get_dummies(df, columns=["Col1"])
    print(df)
    return df


def export_to_csv(df):
    """
    Export a dataframe to a CSV file
    :param df:  to be exported
    :return: None
    """
    df.to_csv("output.csv", index=False)
    return df


if __name__ == '__main__':
    pass